#!/bin/sh
. /usr/local/scripts/build_scripts/util.sh 
source /etc/profile
rep_url="ssh://gerrit@192.168.8.250:29418/android/standard"
jenkins_url="http://192.168.8.246:8080/jenkins/view/Test_Android/"
cur_dir=`pwd`
echo pwd=$cur_dir and var :$1,$2,$3,$4,$5,$6
user=`echo $6 |awk -F "_" '{print $1}'`
echo user=$user
cd $cur_dir
echo git clone $5
git clone $rep_url/$5
cd $5
git checkout $1
git pull
#shell just can return num,not string
check_branch $1
lastest_tag=`cat zbuild_tmp`
echo lastest_tag=$lastest_tag
if [ "$2" == "1"-a "$user" == "admin" ]; then
	echo will push tag to remote
    lastest_tag_num=`echo $lastest_tag |awk -F $origin_tag '{print $2}' |awk -F "." '{print $2}' `
    newest_tag_num=`expr $lastest_tag_num + 1`
    echo newest_tag_num=$newest_tag_num
    newest_tag=$origin_tag."$newest_tag_num"
    echo newest_tag=$newest_tag
	git tag $newest_tag
	git push origin $newest_tag
    echo delete local tag 
    git tag -d $newest_tag
else
	echo just need build Package 
fi

echo pull new tag and code again ....
git checkout $1
git pull
if [ "$2" == "1" -a "$user" == "admin" ];then
    dir_tag=$newest_tag
else
    dir_tag=$lastest_tag
fi
echo dir_tag=$dir_tag
if [ "x$dir_tag" = "x" ];then
	ftp_dir=`echo $1 |sed 's/\.//g'|sed s/^.//g`
else 
	ftp_dir=`echo $dir_tag |awk -F tag_t '{print $2}' |sed 's/\.//g' `
fi
commit=`git rev-parse --short HEAD`
ftp_dir="$user"_"$ftp_dir"_"$commit"
echo ftp_dir=$ftp_dir
pre_build
echo build channel is $3
 case $4 in
	"t_rec_sdk")
	sh "/usr/local/scripts/build_scripts"/test_rec_sdk.sh $1 $3 
	;;
	"t_rec_app" )
	sh "/usr/local/scripts/build_scripts"/test_rec_app.sh $1 $3
	;;
	"t_send_sdk")
	sh "/usr/local/scripts/build_scripts"/test_send_sdk.sh $1 $3
	;;
	"t_send_app")
	sh "/usr/local/scripts/build_scripts"/test_send_app.sh $1 $3
	;;
	*)
	echo please input string, rec_sdk or rec_app or send_sdk or send_app ?
	exit 1
	;;
  esac
#upload dir and files under dir to remote
upload_file $ftp_dir
#保存当前任务的日志，并将日志放到入口job的工作目录下，方便在入口job处直接查看
mv $cur_dir/console_log $cur_dir/"$ftp_dir"_console_log
mv $cur_dir/"$ftp_dir"_console_log /root/.jenkins/workspace/mult_test/
#job构建结束后，删除任务已经中间过程产生的文件夹。
cd /root/.jenkins/workspace/ 
if [ -d $6 ] ;then
    echo delete $6 dir 
    rm $6 -rf
 fi
echo delete job $6
curl -u admin:wangzp@360 -X POST $jenkins_url/job/$6/doDelete 


